//
//  HJBindingAccountController.m
//  KaiChat
//
//  Created by lx on 2024/2/1.
//

#import "HJBindingAccountController.h"
#import "HJTitleTextField.h"
#import "HJMinePresent.h"
#import "QMUIButton.h"
#import "HJTimer.h"
#import "HJPaySetPasswordController.h"

@interface HJBindingAccountController ()

@property (nonatomic, weak) HJStyle1Button *confirmButton;
@property (nonatomic, weak) HJTitleTextField *emailTextField;
@property (nonatomic, weak) HJTitleTextField *codeTextField;
@property (nonatomic, strong) HJTimer *timer;
@property (nonatomic, weak) QMUIButton *sendCodeButton;


@end

@implementation HJBindingAccountController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addBackButton];
        
    UILabel *titleLabel = [UILabel lableWithSuperView:self.view font:UIFontMediumMake(20) color:UIColor.tj_333333Color title:@"Bind mailbox" textAlignment:NSTextAlignmentLeft];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight + 12);
        make.left.offset(leftRightMargin);
    }];
    
    UILabel *subTitleLabel = [UILabel lableWithSuperView:self.view font:UIFontMake(12) color:UIColor.tj_868391Color title:@"Please provide your email for Kai user security verification" textAlignment:NSTextAlignmentLeft];
    [subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(textSubTextLabelMargin);
        make.left.equalTo(titleLabel);
    }];
    
    HJTitleTextField *emailTextField = [HJTitleTextField addToView:self.view];
    emailTextField.titleLabel.text = @"Email Address";
    emailTextField.textField.placeholder = @"Enter your email address";
    emailTextField.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    emailTextField.textField.keyboardType = UIKeyboardTypeASCIICapable;
    [emailTextField.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [emailTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.top.equalTo(subTitleLabel.mas_bottom).offset(50);
        make.height.offset(72);
    }];
    
    HJTitleTextField *codeTextField = [HJTitleTextField addToView:self.view];
    codeTextField.titleLabel.text = @"Verification code";
    codeTextField.textField.placeholder = @"Enter email verification code";
    codeTextField.textField.font = UIFontMake(14);
    codeTextField.textField.maximumTextLength = 6;
    codeTextField.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    codeTextField.textField.keyboardType = UIKeyboardTypeNumberPad;
    [codeTextField.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [codeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.top.equalTo(emailTextField.mas_bottom).offset(24);
        make.height.equalTo(emailTextField);
    }];

    
    QMUIButton *sendCodeButton = [QMUIButton buttonWithSuperView:codeTextField font:UIFontMake(14) color:UIColor.tj_mainColor title:@"Send"];
   [sendCodeButton sizeToFit];
    [sendCodeButton setTitleColor:UIColor.tj_BEBDCAColor forState:UIControlStateDisabled];
    CGFloat sendCodeButtonW = sendCodeButton.bounds.size.width + 10;
    [sendCodeButton addTarget:self action:@selector(didClickSendCodeButton) forControlEvents:UIControlEventTouchUpInside];
    [codeTextField.textField mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.inset(sendCodeButtonW + 15);
    }];
    [sendCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(10);
        make.width.offset(sendCodeButtonW);
        make.top.bottom.equalTo(codeTextField.textField);
    }];
    
    HJStyle1Button *confirmButton = [HJStyle1Button buttonWithSuperView:self.view font:UIFontMediumMake(14) color:nil title:@"Confirm"];
    [confirmButton addTarget:self action:@selector(didClickConfirmButton) forControlEvents:UIControlEventTouchUpInside];
    confirmButton.enabled = NO;
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(20);
        make.height.offset(buttonHeight);
        make.top.equalTo(codeTextField.mas_bottom).offset(30);
    }];
    
    _confirmButton = confirmButton;
    _emailTextField = emailTextField;
    _codeTextField = codeTextField;
    _sendCodeButton = sendCodeButton;
 
}

- (void)didClickSendCodeButton {
    [HJHUD showLoadingInView:self.view];
    [HJMinePresent sendBindingAccountWithEmailCode:_emailTextField.textField.text complete:^(NSError * _Nullable error) {
        if (!error) {
             [self startCountdown];
        }
    }];
}

- (void)startCountdown {
    TJWeakify(self);
    [self.timer start:60 countdownOngoing:^(NSInteger time) {
        weakself.sendCodeButton.enabled = NO;
        [weakself.sendCodeButton setTitle:[NSString stringWithFormat:@"%lus", time] forState:UIControlStateDisabled];
    } countdownEnd:^{
        weakself.sendCodeButton.enabled = YES;
        [weakself.sendCodeButton setTitle:@"Send" forState:UIControlStateNormal];
    }];
}


- (void)textFieldDidChange:(UITextField *)textField {
    _confirmButton.enabled = [_emailTextField.textField.text.lowercaseString containsString:@"@"] && _codeTextField.textField.text.length;
}

- (void)didClickConfirmButton {

    NSString *email = _emailTextField.textField.text;
    if (![email.lowercaseString containsString:@"@"]) {
        [HJHUD showMessage:@"Email format error"];
        return;
    }
    
    [self.view endEditing:YES];

    NSString *emailCode =_codeTextField.textField.text;
    [HJMinePresent bindingAccountWithEmail:email code:emailCode complete:^(NSError * _Nullable error) {
        if (!error) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }];
}
 
- (HJTimer *)timer {
    if (!_timer) {
        _timer = [HJTimer timer];
    }
    return _timer;
}

- (UIModalPresentationStyle)modalPresentationStyle {
    return UIModalPresentationFullScreen;
}

@end
